package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.entity.Role;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.QiniuUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author:ahang
 * @create:2022-07-21 0:07
 * @Description:
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {

    @Reference
    private AdminService adminService;

    @RequestMapping
    public String findPage(Model model, HttpServletRequest request){
        //获取查询条件和分页参数
        Map<String ,Object> filters = super.getFilters(request);
        //调用业务层查询分页数据
        PageInfo<Admin> page = adminService.findPage(filters);
        model.addAttribute("page",page);
        model.addAttribute("filters",filters);
        //页面跳转
        return "admin/index";
    }

    @RequestMapping("/create")
    public String create(){
        return "admin/create";
    }

    @RequestMapping("/save")
    public String insert(Admin admin){
        admin.setHeadUrl("http://47.93.148.192:8080/group1/M00/03/F0/rBHu8mHqbpSAU0jVAAAgiJmKg0o148.jpg");
        adminService.insert(admin);
        return "common/successPage";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id,Model model){
        Admin admin = adminService.getById(id);
        model.addAttribute("admin",admin);
        return "admin/edit";
    }

    @RequestMapping("/update")
    public String update(Admin admin){
        adminService.update(admin);
        return "common/successPage";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        adminService.delete(id);
        return "redirect:/admin";
    }

    @RequestMapping("/uploadShow/{id}")
    public String uploadShow(@PathVariable Long id,Model model){

        model.addAttribute("id",id);
        return "admin/upload";
    }

    /**
     * 上传管理员头像
     * @param id
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/upload/{id}")
    public String upload(@PathVariable Long id, MultipartFile file) throws IOException {//file用来接收上传的图片

        //上传图片到七牛云
        String originalFilename = file.getOriginalFilename();//获取原始的名称
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));//获取后缀名
        String fileName = UUID.randomUUID().toString() + extName;
        QiniuUtil.upload2Qiniu(file.getBytes(),fileName);

        //修改数据库中的admin的头像地址(不是添加)
        Admin admin = new Admin();
        admin.setId(id);
        admin.setHeadUrl("http://rflvieilt.hb-bkt.clouddn.com/"+fileName);
        adminService.update(admin);

        return "common/successPage";
    }

    @Reference
    private RoleService roleService;

    /**
     * 跳转分配权限页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/assignShow/{id}")
    public String assignShow(@PathVariable Long id,Model model){

        model.addAttribute("adminId",id);

        //调用业务层获取已经分配的角色列表和没有分配的角色列表
        //（assignRoleList,[.......]）
        //(noAssignRoleList,[.......])
        Map<String, List<Role>> map = roleService.findRolesByAdminId(id);
        model.addAllAttributes(map);

        return "admin/assignShow";
    }

    /**
     * 给用户分配权限
     * @param adminId
     * @param roleIds
     * @return
     */
    @RequestMapping("/assignRole")
    public String assignRole(Long adminId,Long[] roleIds){

        roleService.saveUserRoleRealtionShip(adminId,roleIds);
        return "common/successPage";
    }


}
